package com.xcj.juhe.tool;

import org.springframework.cglib.beans.BeanMap;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xcj
 * @date 2021/10/15 11:08
 */
public class MyTool {

    /**
     * 将对象装换为map
     *
     * @param bean
     * @return
     */
    public static <T> Map<String, Object> beanToMap(T bean) {
        Map<String, Object> map = new HashMap<>();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                map.put(key + "", beanMap.get(key));
            }
        }
        return map;
    }

    /**
     *  字符串转时间戳
     * @param regTime  入参为“yyyy-MM-dd HH:mm:ss”格式
     * @return
     */
    public String StringToTime(String regTime) {
//        String str = "2019-03-13 13:54:00";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(regTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String ts = String.valueOf(BigDecimal.valueOf(date.getTime()).divide(BigDecimal.valueOf(1000)));
        return ts;
    }

    /**
     * 获取当天的开始时间
     * @param dateStr  入参为“yyyy-MM-dd HH:mm:ss”格式
     * @return
     * @throws ParseException
     */
    public Date getStartTime(String dateStr) throws ParseException {
        Calendar todayStart = Calendar.getInstance();

        SimpleDateFormat dc = new SimpleDateFormat();
        dc.applyPattern("yyyy-MM-dd HH:mm:ss");
        Date date = dc.parse(dateStr);

        todayStart.setTime(date);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 获取当天的结束时间
     * @param dateStr 入参为“yyyy-MM-dd HH:mm:ss”格式
     * @return
     * @throws ParseException
     */
    public Date getEndTime(String dateStr) throws ParseException {
        Calendar todayEnd = Calendar.getInstance();

        SimpleDateFormat dc = new SimpleDateFormat();
        dc.applyPattern("yyyy-MM-dd HH:mm:ss");
        Date date = dc.parse(dateStr);

        todayEnd.setTime(date);
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }

    /**
     * 判断是否为当天
     * @param dateStr  入参为“yyyy-MM-dd HH:mm:ss”格式
     * @return
     */
    public boolean isNowDay(String dateStr){

        Date now = new Date();
        SimpleDateFormat dc = new SimpleDateFormat("yyyy-MM-dd");
        String nowStr = dc.format(now);

        SimpleDateFormat dc2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String inTimeStr = "";
        try {
            Date inTime = dc2.parse(dateStr);
            inTimeStr = dc.format(inTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return nowStr.equals(inTimeStr);
    }

    /**
     * 判断是否为当天
     * @param timeStamp  入参为时间戳格式
     * @return
     */
    public boolean isNowDay2(String timeStamp){

        Date now = new Date();
        SimpleDateFormat dc = new SimpleDateFormat("yyyy-MM-dd");
        String nowStr = dc.format(now);
        System.out.println(nowStr);

        Date inTime = new Date(Long.parseLong(timeStamp)*1000);
        String inTimeStr = dc.format(inTime);
        System.out.println(inTimeStr);

        return nowStr.equals(inTimeStr);
    }

}
